package strings;

import java.util.Arrays;

/**
 * @author pengfei.hpf
 * @date 2020/3/17
 * @verdion 1.0.0
 */
public class CountCharacters {

    public int countCharacters(String[] words, String chars){
        if(words == null || words.length == 0 || chars == null || chars.length() == 0){
            return 0;
        }
        char[] dict = new char[26];
        for(char c: chars.toCharArray()){
            dict[c - 'a'] ++;
        }
        int res = 0;
        for(int i = 0; i < words.length; i ++){
            char[] cs = words[i].toCharArray();
            char[] compare = Arrays.copyOf(dict, 26);
            boolean match = true;
            for(char c: cs){
                if(compare[c -'a'] > 0){
                    compare[c - 'a'] --;
                } else {
                    match = false;
                    break;
                }
            }
            if(match){
                res += cs.length;
            }
        }
        return res;
    }
}
